package com.example.jinzproduct.server.impl;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.commons.entity.ConsultationsDO;
import com.example.commons.entity.DictDO;
import com.example.commons.entity.RoleConsultationsDO;
import com.example.commons.entity.UserDO;
import com.example.jinzproduct.RedisUtil.RedisTime;
import com.example.jinzproduct.RedisUtil.RedisUtil;
import com.example.jinzproduct.dao.ConsultationsDao;
import com.example.jinzproduct.dao.DictDao;
import com.example.jinzproduct.dao.RoleConsultationsDao;
import com.example.jinzproduct.server.ConsultationsService;
import com.example.jinzproduct.server.DictService;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.*;

/**
 *
 * <pre>
 * 资讯表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Service
@AllArgsConstructor
public class ConsultationsServiceImpl extends ServiceImpl<ConsultationsDao, ConsultationsDO> implements ConsultationsService {
	private final RoleConsultationsDao roleConsultationsMapper;

    @Resource
    private RedisUtil redisUtil;

    @Transactional
    @Override
    public boolean insert(ConsultationsDO consultations) {

        int count = baseMapper.insert(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }

        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }

        return retBool(count);
    }

    @Transactional
    @Override
    public boolean update(ConsultationsDO consultations) {

        int count = baseMapper.updateById(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }

        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }
     HashMap map=   Maps.newHashMapWithExpectedSize(7);

        return retBool(count);
    }

    /**
     * 		switch (c.getState()){
     * 				case 0:exportVo.setState("未开始");break;
     * 				case 1:exportVo.setState("已考试");break;
     * 				case 2:exportVo.setState("已收款");break;
     * 				case 3:exportVo.setState("个人已返款");break;
     * 				case 4:exportVo.setState("学校已返款");break;
     * 				case 5:exportVo.setState("已完成");break;
     */

    @Override
    public Map findList(int current, int number, String secondColumn) {
        String rediskey=current+number+secondColumn;

        Map map1=   (Map) redisUtil.get(rediskey);
        if(map1!=null){
            return map1;
        }

        Map map =new HashMap<>();
        // 新建分页

        Page<ConsultationsDO> page = new Page<ConsultationsDO>(current, number);
        if(StringUtils.isBlank(secondColumn)){
            List<ConsultationsDO> list1=   this.baseMapper.findList(page , "政策信息");
            QueryWrapper<ConsultationsDO> wrapper = new QueryWrapper<ConsultationsDO>().orderByAsc("id");
            wrapper.like("second_column","政策信息");
            wrapper.like("state",1);
            List<ConsultationsDO> list2=   this.baseMapper.findList(page , "通知公告");
            QueryWrapper<ConsultationsDO> wrapper1 = new QueryWrapper<ConsultationsDO>().orderByAsc("id");
            wrapper1.like("second_column","通知公告");
            wrapper1.like("state",1);
            QueryWrapper<ConsultationsDO> wrapper2 = new QueryWrapper<ConsultationsDO>().orderByAsc("id");
            List<ConsultationsDO> list3=   this.baseMapper.findList(page , "试点院校风采");
            wrapper2.like("second_column","试点院校风采");
            wrapper2.like("state",1);
            List<ConsultationsDO> list4=   this.baseMapper.findList(page , "工作动态");
            QueryWrapper<ConsultationsDO> wrapper3 = new QueryWrapper<ConsultationsDO>().orderByAsc("id");
            wrapper3.like("second_column","工作动态");
            wrapper3.like("state",1);
            map.put("indexList3",list1);
            map.put("indexList3Count",baseMapper.selectCount(wrapper));
            map.put("indexList2",list2);
            map.put("indexList2Count",baseMapper.selectCount(wrapper1));
            map.put("indexList1",list3);
            map.put("indexList1Count", baseMapper.selectCount(wrapper2));
            map.put("indexList",list4);
            map.put("indexListCount",baseMapper.selectCount(wrapper3));

             redisUtil.set(rediskey,map, RedisTime.min30);
         return    map;
        }else {
            //政策信息  行业动态（通知公告）  考评动态 （试点院校风采）  企业动态（工作动态）
            switch (secondColumn){
                case "政策信息" : secondColumn="政策信息";
                    break;
                case "行业动态" : secondColumn="通知公告";
                    break;
                case "考评动态" : secondColumn="试点院校风采";
                    break;
                case "企业动态" : secondColumn="工作动态";
                    break;
            }
            List<ConsultationsDO>  lis=   this.baseMapper.findList(page , secondColumn);
             page.setRecords(lis);

            map.put("indexList",page.getRecords());

            QueryWrapper<ConsultationsDO> wrapper = new QueryWrapper<ConsultationsDO>().orderByAsc("id");
            wrapper.like("second_column",secondColumn);
            wrapper.like("state",1);
            map.put("indexListCount",baseMapper.selectCount(wrapper));
            redisUtil.set(rediskey,map, RedisTime.min30);
            return    map;
        }
    }



}
